$(document).ready(function(e) {	
	if($G['func']=='init'){
		if($G['part'] == 'market'){
			$.ajax({
				url: 'https://api.bosscms.net/rest/user/test.php?auth=free',
				error: function(){
					window.location.href = $.mpf('plugin','plugin','init');
				}
			});
		}
		
		$.post('https://api.bosscms.net/rest/user/identity.php?auth=free',{'user_sequence':$G['user']},function(data){
			$('body').prepend( sethtml.identity(data) );
			$('.market>.filter>dl>dd>form').after(
				'<a class="collect '+($.request('collect')=='list'?'red':'')+'"'+
				(data.money?'href="'+$.mpf('plugin','market','init',($.request('collect')=='list'?null:{'collect':'list'}))+'"':'href="javascript:_alert(\'请先登录官方账号\');"')+
				'><i class="fa fa-star"></i>我的收藏</a>'
			);
			var url = $.paramobj('https://api.bosscms.net/rest/user/app.php?auth=free',{
				'pages': $.request('pages'),
				'divide': $.request('divide'),
				'collect': $.request('collect'),
				'search': $.request('search')
			}), param = window.location.href.match(/param,\d+/g);
			for(i in param){
				url = $.params(url,param[i],$.request(param[i]));
			}
			if($G['part'] == 'market'){
				$.post(url,{'user_sequence':$G['user']},function(res){
					plugins = JSON.parse($('.market>.list').attr('plugins') || '[]');
					html = $('tr[thead]')[0].outerHTML;
					for(k in res.list){
						v = res.list[k];
						html += '<tr><td><div class="vimg"><img src="'+v['image']+'" alt="'+v['title']+'" /><span><b version="'+v['version']+'" name="'+v['name']+'">';
						if(v['link']){
							html += '<a href="'+v['link']+'" target="_blank">'+v['title']+'</a>';
						}else{
							html += v['title'];
						}
						html += '</b><p>'+v['description']+'</p></span></div></td><td width="98">'+v['author']+'</td><td width="98">'+v['version']+'</td><td width="98" price class="nowrap">';
						if(v['groups']==0){
							html += '免费';
						}else if(v['price']==0){
							html += v['remark'];
						}else{
							html += '<span class="fa fa-rmb money">'+v['price']+'</span> / '+v['buy_duration'];
							if(v['remark']){
								html += '<a href="javascript:_alert(\''+v['remark']+'\',\'gold\');" color="red" class="preferential">[优惠]</a>';
							}
							html += ' &nbsp; &nbsp;';
						}
						html += '</td><td width="98">';
						if(plugins.hasOwnProperty(v['name']) && plugins[v['name']]['must']){
							html += '<code class="radio">'
									+  '<div>'
									+  '<ul class="'+(plugins[v['name']]['must']*1?'on':'')+'">'
									+  '<li value="1">启用</li>'
									+  '<li value="0">关闭</li>'
									+  '</ul>'
									+  '</div>'
									+  '<textarea  name="must" type="radio" id="'+plugins[v['name']]['id']+'">'+plugins[v['name']]['must']+'</textarea>'
									+  '</code>';
						}
						html += '</td><td width="98">';
						if(plugins.hasOwnProperty(v['name']) && plugins[v['name']]['display']){
							html += '<code class="radio green">'
									+  '<div>'
									+  '<ul class="'+(plugins[v['name']]['display']*1?'on':'')+'">'
									+  '<li value="1">启用</li>'
									+  '<li value="0">关闭</li>'
									+  '</ul>'
									+  '</div>'
									+  '<textarea  name="display" type="radio" id="'+plugins[v['name']]['id']+'">'+plugins[v['name']]['display']+'</textarea>'
									+  '</code>';
						}
						html += '</td><td width="188" operate>';
						if(plugins.hasOwnProperty(v['name'])){
							if(plugins[v['name']]['id']){
								html += '<a class="btnfa blue" href="'+$.mpf(v['name'],v['name'],'init')+'"'+v['target']+'><em class="fa fa-pencil" title="管理"></em></a> '
										+  '<a class="btnfa red uninstall" url="'+$.mpf(v['name'],'uninstall','init')+'"><em class="fa fa-trash-o" title="卸载"></em></a> ';
							}else{
								html += '<a class="btnfa green" href="'+$.mpf(v['name'],'install','init')+'" install="'+v['name']+'"><em class="fa fa-cog" title="安装"></em></a> '
										+  '<a class="btnfa red delete" url="'+$.mpf('plugin','plugin','delete',{'name':v['name']})+'"><em class="fa fa-trash-o" title="删除"></em></a> ';
							}
						}else{
							if(v['groups']==0 || v['buyed']){
								html += '<a class="btnfa green" ';
								if(data.money){
									html += 'url="'+$.mpf('plugin','market','install',{'name':v['name']})+'" install="'+v['name']+'" groups="'+v['groups']+'"';
								}else{
									html += 'href="javascript:_alert(\'请先登录官方账号\');"';
								}
								html += '><em class="fa fa-cloud-download" title="安装"></em></a> ';
							}else{
								html += '<a class="btnfa green buy" ';
								if(data.money){
									if(v['price']==0){
										html += 'href="javascript:_alert(\''+v['remark']+'\');"';
									}else{
										html += 'easy="nofull" width="580" height="420" name="购买 “'+v['title']+'”" url="'+$.mpf('plugin','market','info',{'id':v['id'],'name':v['name']})+'"';
									}
								}else{
									html += 'href="javascript:_alert(\'请先登录官方账号\');"';
								}
								html += '><em class="fa fa-pencil" title="购买"></em></a> ';
							}
							if(v['collect']){
								html += '<a class="btnfa red" ';
								if(data.money){
									html += 'href="'+$.mpf('plugin','market','collect',{'id':v['id']})+'"';
								}else{
									html += 'href="javascript:_alert(\'请先登录官方账号\');"';
								}
								html += '><em class="fa fa-star" title="已收藏"></em></a> ';
							}else{
								html += '<a class="btnfa blue" ';
								if(data.money){
									html += 'href="'+$.mpf('plugin','market','collect',{'id':v['id']})+'"';
								}else{
									html += 'href="javascript:_alert(\'请先登录官方账号\');"';
								}
								html += '><em class="fa fa-star-o" title="收藏"></em></a> ';
							}
						}
						html += '</td></tr>';
					}
					$('.market>.list>table>tbody').html(html);
					initcode($('.market>.list>table>tbody'));
					rows = $.getCookie($G['mold']+'_'+$G['part']+'_rows');
					$('.market>.pages>ol').html( sethtml.pages(res.pages, res.total, -1) );
					operate(data);
				},'json');
			}else if($G['part'] == 'plugin'){
				$.post($.params(url,'names',$('.market>.list').attr('names')),{'user_sequence':$G['user']},function(res){
					for(k in res.list){
						html = '';
						v = res.list[k];
						if(v['groups']==0){
							html += '免费';
						}else{
							html += '<span class="fa fa-rmb money">'+v['price']+'</span> / '+v['buy_duration'];
							if(v['remark']){
								html += '<a href="javascript:_alert(\''+v['remark']+'\',\'gold\');" color="red" class="preferential">[优惠]</a>';
							}
							html += ' &nbsp; &nbsp;';
						}
						$('b[name="'+v['name']+'"]').parents('tr').find('td[price]').html(html);
					}
				},'json');
				operate(data);
			}
		},'json');
		$.get($.params('https://api.bosscms.net/rest/user/field.php?auth=free','divide',$.request('divide')),function(res){
			html =  '<li class="'+(!$.request('divide')&&$G['part']!='plugin'?'on':'')+'"><a href="'+$.mpf('plugin','market','init')+'">全部</a></li>'+
					'<li class="'+($G['part']=='plugin'?'on':'')+'"><a href="'+$.mpf('plugin','plugin','init')+'">已安装</a></li>';
			for(k in res.app.divide){
				html += '<li class="'+($.request('divide')==k?'on':'')+'"><a href="'+$.mpf('plugin','market','init',{'divide':k})+'">'+res.app.divide[k]+'</a></li>';
			}
			$('.market>.cut>ul').html(html);
			html = '';
			for(k in res.app.complex){
				v = res.app.complex[k];
				html += '<dl><dt>'+v['title']+'：</dt><dd class="param">'+
						'<a class="'+(!$.request(v['name'])?'on':'')+'" href="'+$.params($.paramobj(window.location.href,{'part':'market','pages':null}),v['name'],null)+'">全部</a>';
				for(k2 in v['param']){
					s = v['param'][k2];
					html += '<a class="'+($.request(v['name'])==s?'on':'')+'" href="'+$.params($.paramobj(window.location.href,{'part':'market','pages':null}),v['name'],encodeURI(s))+'">'+s+'</a>';
				}
				html += '</dd></dl>';
			}
			$('.market>.filter').prepend(html);
		},'json');
		

		function operate(data){
			$.post($.paramobj('https://api.bosscms.net/rest/user/update.php?auth=free',{
				'type': 'app',
				'names': $('.market>.list').attr('names')
			}),{'user_sequence':$G['user']},function(res){
				for(k in res){
					if(res[k] > $('b[name="'+k+'"]').attr('version')){
						$('b[name="'+k+'"]').parents('tr').find('td[operate]').prepend(
							'<a class="btnfa green" '+
							(data.money?'href="'+$.mpf('plugin','market','install',{'name':k,'update':res[k]})+'"':'href="javascript:_alert(\'请先登录官方账号\');"')+
							'><em class="fa fa-cloud-upload" title="升级"></em></a>'
						);
					}
				}
			},'json');
		}
	}

	$(document).on('change','input[name="pi"][type="radio"]',function(){
		$('dl.nid').removeClass('on').find('label input[value=0]').click();
		$('dl.nid[nid="'+$(this).val()+'"]').addClass('on');
		price = $(this).attr('price');
		$('label.price i').html(price);
		$('label.price input').val(price).attr('price',price);
	}).on('change','input[name^="coupon"][type="radio"]',function(){
		if($(this).prop('checked')){
			the = $('label.price input');
			if(!(inum = $(this).parent('label').find('.coupon u').html())){
				inum = 0;
			}
			price = the.attr('price')-inum;
			price = price>0?price:0;
			$('label.price i').html(price);
			the.val(price);
		}	
	}).on('change','input[name="pay"][type="radio"]',function(){
		if($(this).val()=='balance'){
			$('dl.password').removeAttr('hide');
			$(window).scrollTop(999);
		}else{
			$('dl.password').attr('hide','hide');
		}
	}).on('submit','form#buy',function(){
		if(!$('input[name="protocol"][type="checkbox"]').is(':checked')){
			_alert('请阅读协议并点击确认');
			return false;
		}
		if($('input[name="pay"][type="radio"]:checked').val()=='balance' && $('dl.password input[name="password"]').val()==''){
			_alert('请填写官方账号密码');
			$('dl.password input[name="password"]').focus();
			return false;
		}
	}).on('click','a[url][install]',function(){
		iname = $(this).attr('install');
		if($(this).attr('groups')==0){
			window.location.href = $(this).attr('url');
			$('section.category li.plugin',window.top.document).addClass(iname);
		}else{
			if($('section.easy.install').length==0){
				$('body').append('<section class="easy install">'+
				  '<div class="window">'+
					'<div class="icon">'+
					  '<span class="close "><em class="fa fa-times"></em></span>'+
					'</div>'+
					'<div class="move">授权域名</div>'+
					'<iframe></iframe>'+
				  '</div>'+
				'</section>');
			}
			$('section.easy.install div.window')
			  .css({'width':680,'height':440,'left':$('section.easy.install').width()/2-340,'top':$('section.easy.install').height()/2-220,})
			  .find('iframe').attr('src',$.mpf('plugin','market','inst',{name:iname}));
		}
	}).on('click','div.order a.install',function(){
		$('section.category li.plugin',window.top.document).addClass($(this).attr('install'));
	}).on('click','a.btnfa.red.uninstall',function(){
		if(confirm('是否卸载该应用？')){
			vb = $(this).parents('tr').find('div.vimg>span>b');
			vname = vb.attr('name');
			$('section.category>.nav>ul>li>ul>li.plugin',window.top.document).removeClass(vb.attr('name'));
			$('section.category>.nav>ul[nav][plugin]>li>ul>li.'+vname,window.top.document).remove();
			window.location.href = $(this).attr('url');
		}
	}).on('change','td[width="98"] textarea[name="must"]',function(){
		(function(the){
			obj = {
				'id': the.attr('id'),
				'must': the.val()
			}
			$.post($.mpf('plugin','plugin','modify',{type:'must',jsonmsg:1}),obj,function(res){
				if(res.msg==0){
					mustfunc(the,res.msg);
					_alert('关闭成功','green');
				}else if(res.msg==1){
					mustfunc(the,res.msg);
					_alert('启用成功','green');
				}else{
					_alert(res.msg);
				}
			},'json');
		})($(this));
	}).on('change','td[width="98"] textarea[name="display"]',function(){
		(function(the){
			obj = {
				'id': the.attr('id'),
				'display': the.val()
			}
			$.post($.mpf('plugin','plugin','modify',{type:'display',jsonmsg:1}),obj,function(res){
				if(res.msg==0){
					vb = the.parents('tr').find('div.vimg>span>b');
					vname = vb.attr('name');
					$('section.category>.nav>ul>li>ul>li.plugin',window.top.document).removeClass(vb.attr('name'));
					$('section.category>.nav>ul[nav][plugin]>li>ul>li.'+vname,window.top.document).remove();
					if($('section.category>.nav>ul[nav][plugin]>li>ul>li',window.top.document).length==0){
						$('header.topnav>.column>ul>li[class^="nav"][plugin]',window.top.document).addClass('hide');
					}
					_alert('关闭成功','green');
				}else if(res.msg==1){
					_alert('启用成功','green');
					mustfunc(the, $('td[width="98"] textarea[name="must"][id="'+the.attr('id')+'"]').val());
				}else{
					_alert(res.msg);
				}
			},'json');
		})($(this));
	});

	if($('.order a.install').length>0){
		$('.order a.install').attr('href', $('.order a.install').attr('href').replace('referer=[url]','referer='+encodeURIComponent(window.parent.window.location.href)));
	}
	if($('section.main.bind').length>0){
		$('section.easy.install',window.parent.document).addClass('active');
		$('section.main.bind a.auth[url]').click(function(){
			if(confirm('请确认该软件产品安装到本站')){
				window.location.href = $(this).attr('url');
			}
		});
	}
	$('td[width="98"] textarea[name="must"][id]').each(function(){
		mustfunc($(this), $(this).val());
	});
	function mustfunc(the, st){
		vb = the.parents('tr').find('div.vimg>span>b');
		vname = vb.attr('name');
		if(st==0){
			$('section.category>.nav>ul>li>ul>li.plugin',window.top.document).addClass(vname);
			$('section.category>.nav>ul[nav][plugin]>li>ul>li.'+vname,window.top.document).remove();
		}else if(st==1){
			$('section.category>.nav>ul>li>ul>li.plugin',window.top.document).removeClass(vb.attr('name'));
			if($('section.category>.nav>ul[nav][plugin]>li>ul>li.'+vname,window.top.document).length==0){
				$('section.category>.nav>ul[nav][plugin]>li>ul',window.top.document).append(
					'<li class="'+vname+'"><a href="'+$.mpf(vname,vname,'init')+'" name="'+$('section.category>.nav>ul[nav][plugin]',window.top.document).attr('name')+'-'+vb.text()+'" target="iframe">'+vb.text()+'</a></li>');
			}
		}
		if($('section.category>.nav>ul[nav][plugin]>li>ul>li',window.top.document).length>0){
			$('section.category>.nav>ul[nav][plugin]',window.top.document).removeAttr('hide');
		}else{
			$('section.category>.nav>ul[nav][plugin]',window.top.document).attr('hide','hide');
		}
	}
});